package com.yuanye.yeecho.base;

import android.os.Handler;
import android.os.Message;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;

import androidx.fragment.app.Fragment;

import com.yuanye.yeecho.bean.KeyDataBean;
import com.yuanye.yeecho.utils.YY;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseFragment extends Fragment {
    public static final int PASS = 0;
    public static final int FAIL = 1;
    public static final int NONE = -1;
    public static final int TESTING = 2;

    private boolean isHideButton;

    protected Handler handler;

    //测试项ID
    private int taskId;
    //测试项名称
    private String taskName = "未设置测试项名称";
    //测试项描述
    private String taskDesc;
    //测试开始时间
    private long startTimeMills = 0;
    //测试时长
    private long testDuration = 0;
    //测试结果
    private int state = NONE;
    //测试关键数据集
    public List<KeyDataBean> keyDataList = new ArrayList<>();

    public BaseFragment(Handler handler) {
        this.handler = handler;
    }

    public BaseFragment() {}

    public abstract void execTask();

    public abstract void stopTask();

    public int getTaskId() {
        return taskId;
    }

    public void setTaskId(int taskId) {
        this.taskId = taskId;
    }

    public String getTaskName() {
        return taskName;
    }

    public void setTaskName(String taskName) {
        this.taskName = taskName;
    }

    public String getTaskDesc() {
        return taskDesc;
    }

    public void setTaskDesc(String taskDesc) {
        this.taskDesc = taskDesc;
    }

    public long getStartTimeMills() {
        return startTimeMills;
    }

    public void setStartTimeMills(long startTimeMills) {
        this.startTimeMills = startTimeMills;
    }

    public long getTestDuration() {
        return testDuration;
    }

    public void setTestDuration(long testDuration) {
        this.testDuration = testDuration;
    }

    public int getState() {
        return state;
    }

    public void setState(int result) {
        this.state = result;
    }

    protected void setHandler(Handler handler){
        this.handler = handler;
    }

    protected void setPass(){
        stopTask();
        setState(PASS);
        if (handler != null){
            Message msg = Message.obtain();
            msg.what = Yeecho.PASS;
            msg.obj = getTaskId();
            handler.sendMessage(msg);
        }
    }

    protected void setFail(){
        stopTask();
        setState(FAIL);
        if (handler != null){
            Message msg = Message.obtain();
            msg.what = Yeecho.FAIL;
            msg.obj = getTaskId();
            handler.sendMessage(msg);
        }
    }

    protected void runInBackground(){
        setState(TESTING);
        if (handler != null){
            Message msg = Message.obtain();
            msg.what = Yeecho.NEXT;
            msg.obj = getTaskId();
            handler.sendMessage(msg);
        }
    }

    protected void passAndNext(){
        if (getState() == TESTING){
            setPass();
        }else{
            stopTask();
            setState(PASS);
            if (handler != null){
                Message msg = Message.obtain();
                msg.what = Yeecho.PASS_AND_NEXT;
                msg.obj = getTaskId();
                handler.sendMessage(msg);
            }
        }
    }

    protected void goToNext(){
        if (handler != null){
            Message msg = Message.obtain();
            msg.what = Yeecho.NEXT;
            msg.obj = getTaskId();
            handler.sendMessage(msg);
        }
    }

    protected void lock(){
        if (handler != null){
            handler.sendEmptyMessage(Yeecho.LOCK);
        }
    }

    protected void unlock(){
        if (handler != null){
            handler.sendEmptyMessage(Yeecho.UNLOCK);
        }
    }

    protected void showButton(boolean enable){
        if (handler != null){
            handler.sendEmptyMessage(enable ? Yeecho.SHOW_TEST_BUTTON : Yeecho.HIDE_TEST_BUTTON);
        }
    }

    protected void setStatusMsg(String content){
        if (handler != null){
            Message msg = Message.obtain();
            msg.what = Yeecho.UPDATE_STATUS;
            msg.obj = content;
            handler.sendMessage(msg);
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (isHideButton)
            showButton(hidden);
    }

    protected void hideTestButton(){
        isHideButton = true;
    }

    protected SpannableString getSpanStr(String content, int color){
        SpannableString str = new SpannableString(content);
        str.setSpan(new ForegroundColorSpan(color), 0, str.length(),  Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return str;
    }

    protected void addKeyData(String keyName, String keyValue){
        addKeyData(keyName, keyValue, "");
    }

    protected void addKeyData(String keyName, String keyValue, String desc){
        addKeyData(keyName, keyValue, desc, true);
    }

    protected void addKeyData(String keyName, String keyValue,  String desc, boolean result){
        KeyDataBean bean = new KeyDataBean(keyName);
        bean.setResValue(keyValue);
        bean.setRes(result ? 0 : 1);
        bean.setResDesc(desc);
        bean.setUnitName(getTaskName());
        bean.setTestTime(YY.simpleDateFormat.format(System.currentTimeMillis()));
        keyDataList.add(bean);
    }

}
